package com.penghe.www.taiwanmap.been

import android.graphics.*
import android.support.annotation.ColorInt

/**
 * Created by pengh on 2017/6/1.
 *
 * 代表一个单独的市
 */

class CityItem constructor(path: Path) {

    /**
     * 城市的绘制路径
     */
    private val mPath: Path = path

    /**
     * 颜色
     */
    @ColorInt
    private var drawColor: Int = 0

    /**
     * 自己绘制出自己的图形
     *
     * @param canvas 画布
     * @param paint 画笔
     * @param isSelect 当前的市 是否被选中
     */
    fun draw(canvas: Canvas, paint: Paint, isSelect: Boolean) {
        if (isSelect) {

            //绘制城市的背景
            paint.strokeWidth = 2f
            paint.color = Color.BLACK
            paint.style = Paint.Style.FILL
            paint.setShadowLayer(8f, 0f, 0f, 0xFFFFFF)

            canvas.drawPath(mPath, paint)

            //绘制城市
            paint.clearShadowLayer()
            paint.color = drawColor
            paint.style = Paint.Style.FILL
            paint.strokeWidth = 2f
            canvas.drawPath(mPath, paint)
        } else {

            //绘制城市
            paint.clearShadowLayer()
            paint.strokeWidth = 1f
            paint.style = Paint.Style.FILL
            paint.color = drawColor
            canvas.drawPath(mPath, paint)

            //绘制边线背景
            paint.style = Paint.Style.STROKE
            paint.color = Color.BLACK
            canvas.drawPath(mPath, paint)
        }
    }

    /**
     * 根据屏幕坐标，判断点击位置是否属于自己
     *
     * @param x 被点击的屏幕X坐标
     * @param y 被点击的屏幕Y坐标
     * @return true表示点击的位置属于当前的城市
     */
    fun isTouch(x: Int, y: Int): Boolean {
        //创建矩形区域
        val recf: RectF = RectF()
        //将这个不规则的图形放到区域中
        mPath.computeBounds(recf, true)
        val region = Region()
        //裁剪掉mPath区域之外的部分（获取mPath的区域）
        region.setPath(mPath, Region(recf.left.toInt(), recf.top.toInt(), recf.right.toInt(), recf.bottom.toInt()))
        //判断坐标是否在图形之内
        return region.contains(x, y)
    }

    fun setDrawColor(color: Int) {
        drawColor = color
    }
}